package com.kuwou.workobject.Utils;

import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.kuwou.workobject.pojo.entity.Artist;
import com.kuwou.workobject.pojo.entity.SingerBasicInfo;
import com.kuwou.workobject.pojo.entity.SingerDetailInfo;
import com.kuwou.workobject.pojo.entity.SingerList;
import com.kuwou.workobject.service.SingerBasicInfoService;
import com.kuwou.workobject.service.SingerDetailInfoService;
import com.kuwou.workobject.service.SingerListService;

import java.util.ArrayList;
import java.util.List;

public class HandleSingerUtils {

    private static SingerListService singerListService;
    private static SingerDetailInfoService singerDetailInfoService;
    private static SingerBasicInfoService singerBasicInfoService;

    /**
     * 设置SingerListService实现
     *
     * @param singerListService
     */
    public static void setSingerListService(SingerListService singerListService) {
        HandleSingerUtils.singerListService = singerListService;
    }

    /**
     * 设置SingerDetailInfoService实现
     *
     * @param singerDetailInfoService
     */
    public static void setSingerDetailInfoService(SingerDetailInfoService singerDetailInfoService) {
        HandleSingerUtils.singerDetailInfoService = singerDetailInfoService;
    }

    /**
     * 获取SingerBasicInfoService实现
     *
     * @param singerBasicInfoService
     */
    public static void setSingerBasicInfoService(SingerBasicInfoService singerBasicInfoService) {
        HandleSingerUtils.singerBasicInfoService = singerBasicInfoService;
    }

    //清空歌手表
    public static void tTBySingerList(){
        if (singerListService == null) {
            throw new IllegalStateException("ServiceDelegate未初始化，请先调用setServiceDelegate注入实现");
        }
        singerListService.truncateTable();
    }

    //清空歌手详细信息表
    public static void tTBySingerDetailInfo(){
        if (singerDetailInfoService == null) {
            throw new IllegalStateException("ServiceDelegate未初始化，请先调用setServiceDelegate注入实现");
        }
        singerDetailInfoService.truncateTable();
    }

    //清空歌手基础信息表
    public static void tTBySingerBasicInfo(){
        if (singerBasicInfoService == null) {
            throw new IllegalStateException("ServiceDelegate未初始化，请先调用setServiceDelegate注入实现");
        }
        singerBasicInfoService.truncateTable();
    }

    //插入歌手
    public static List<Artist> saveSingerBatch(List<Artist> singerList){
        List<SingerList> singers = new ArrayList<>();
        singerList.forEach(artist -> {
            singers.add(new SingerList(artist.getId(), artist.getName()));
        });
        Db.saveOrUpdateBatch(singers);
        int i = 0;
        for (SingerList singer : singers) {
            singerList.get(i).setSingerId(singer.getId());
            i++;
        }
        return singerList;
    }

    //插入歌手详细信息
    public static int saveSingerDetailInfoBatch(List<SingerDetailInfo> SingerDetailInfo){
        try{
            Db.saveOrUpdateBatch(SingerDetailInfo);
            return 1;
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }

    }

    //插入歌手基础信息
    public static int saveSingerBasicInfoBatch(List<SingerBasicInfo> singerBasicInfo) {
        try{
            Db.saveOrUpdateBatch(singerBasicInfo);
            return 1;
        }catch (Exception e){
            e.printStackTrace();
            return 0;
        }
    }
}
